"""
测试配置文件

包含测试环境所需的各种配置参数。
"""

import os
from pathlib import Path


class TestConfig:
    """测试配置类"""

    # 路径配置
    TEST_DIR = Path(__file__).parent.parent
    PROJECT_ROOT = TEST_DIR.parent
    SRC_DIR = PROJECT_ROOT / "src"

    # API 配置
    API_BASE_URL = os.getenv("API_BASE_URL", "http://localhost:9999")
    API_TIMEOUT = int(os.getenv("API_TIMEOUT", "30"))
    API_RETRY_ATTEMPTS = int(os.getenv("API_RETRY_ATTEMPTS", "3"))

    # 前端配置
    FRONTEND_BASE_URL = os.getenv("FRONTEND_BASE_URL", "http://localhost:3001")
    FRONTEND_TIMEOUT = int(os.getenv("FRONTEND_TIMEOUT", "10"))

    # 测试配置
    DEFAULT_TIMEOUT = int(os.getenv("DEFAULT_TIMEOUT", "10"))
    POLL_INTERVAL = float(os.getenv("POLL_INTERVAL", "0.5"))
    MAX_RETRIES = int(os.getenv("MAX_RETRIES", "3"))

    # 测试数据配置
    DEFAULT_PRODUCT = os.getenv("DEFAULT_PRODUCT", "aln")
    DEFAULT_VERSION = os.getenv("DEFAULT_VERSION", "1119")
    TEST_MODULE_ID = os.getenv("TEST_MODULE_ID", "4876")

    # 浏览器配置
    BROWSER_HEADLESS = os.getenv("BROWSER_HEADLESS", "true").lower() == "true"
    BROWSER_WINDOW_WIDTH = int(os.getenv("BROWSER_WINDOW_WIDTH", "1920"))
    BROWSER_WINDOW_HEIGHT = int(os.getenv("BROWSER_WINDOW_HEIGHT", "1080"))
    BROWSER_DOWNLOAD_DIR = str(TEST_DIR / "downloads")

    # 数据库配置 (如果需要)
    TEST_DB_URL = os.getenv("TEST_DB_URL", "sqlite:///test.db")

    # 日志配置
    LOG_LEVEL = os.getenv("LOG_LEVEL", "INFO")
    LOG_FORMAT = "%(asctime)s - %(name)s - %(levelname)s - %(message)s"

    # 报告配置
    REPORTS_DIR = TEST_DIR / "reports"
    SCREENSHOT_DIR = REPORTS_DIR / "screenshots"
    HTML_REPORT_DIR = REPORTS_DIR / "html"
    COVERAGE_DIR = REPORTS_DIR / "coverage"

    # 性能测试配置
    PERFORMANCE_TIMEOUT = int(os.getenv("PERFORMANCE_TIMEOUT", "60"))
    PERFORMANCE_SAMPLES = int(os.getenv("PERFORMANCE_SAMPLES", "5"))

    # 并发测试配置
    PARALLEL_WORKERS = int(os.getenv("PARALLEL_WORKERS", "4"))
    PARALLEL_LOAD_DELAY = float(os.getenv("PARALLEL_LOAD_DELAY", "0.1"))

    # 模拟数据配置
    MOCK_DATA_SIZE = int(os.getenv("MOCK_DATA_SIZE", "20"))
    MOCK_COMPLEX_DATA_SIZE = int(os.getenv("MOCK_COMPLEX_DATA_SIZE", "100"))

    # CORS 配置
    CORS_ORIGINS = ["http://localhost:3001", "http://127.0.0.1:3001"]
    CORS_METHODS = ["GET", "POST", "PUT", "DELETE", "OPTIONS"]

    # 错误处理配置
    MAX_RETRY_DELAY = float(os.getenv("MAX_RETRY_DELAY", "5.0"))
    BACKOFF_FACTOR = float(os.getenv("BACKOFF_FACTOR", "2.0"))

    @classmethod
    def get_api_url(cls, endpoint: str) -> str:
        """获取完整的 API URL"""
        return f"{cls.API_BASE_URL}{endpoint}"

    @classmethod
    def get_frontend_url(cls, path: str = "") -> str:
        """获取完整的前端 URL"""
        return f"{cls.FRONTEND_BASE_URL}{path}"

    @classmethod
    def get_browser_options(cls):
        """获取浏览器选项"""
        options = []
        if cls.BROWSER_HEADLESS:
            options.extend(["--headless"])
        options.extend([
            f"--window-size={cls.BROWSER_WINDOW_WIDTH},{cls.BROWSER_WINDOW_HEIGHT}",
            "--no-sandbox",
            "--disable-dev-shm-usage",
            "--disable-gpu",
            "--disable-web-security",
            "--disable-features=VizDisplayCompositor"
        ])
        return options

    @classmethod
    def ensure_directories(cls):
        """确保必要的目录存在"""
        directories = [
            cls.REPORTS_DIR,
            cls.SCREENSHOT_DIR,
            cls.HTML_REPORT_DIR,
            cls.COVERAGE_DIR,
            cls.BROWSER_DOWNLOAD_DIR
        ]
        for directory in directories:
            directory.mkdir(parents=True, exist_ok=True)

    @classmethod
    def is_ci_environment(cls) -> bool:
        """检查是否在 CI 环境中运行"""
        return os.getenv("CI", "false").lower() == "true"

    @classmethod
    def get_test_data_path(cls, filename: str) -> Path:
        """获取测试数据文件路径"""
        return cls.TEST_DIR / "data" / filename